﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.IO;
using System.Drawing;
using Utility1;

namespace Thv.trangchu.canhan
{
    public partial class myInfor : System.Web.UI.Page
    {
        const string UploadDirectory = "~/trangchu/canhan/photo/";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserDB u = (UserDB)Session[Global.SESS_SECURITY_CODE];
                hdfUserID.Value = u.ID.ToString();
                binngaysinh();
                bindata();
            }
        }
        void binngaysinh()
        {
            for (int i = 1; i <= 31; i++)
                cbNgay.Items.Add(i.ToString(), i);
            cbNgay.SelectedIndex = 0;
            for (int i = 1; i <= 12; i++)
                cbThang.Items.Add("Tháng " + i, i);
            cbThang.SelectedIndex = 0;
            for (int i = 1950; i <= 2010; i++)
                cbNam.Items.Add("Năm " + i, i);
            cbNam.SelectedIndex = 20;
        }
        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            UserDB u = new UserDB();
            u = dx.UserDBs.Single(p => p.ID == int.Parse(hdfUserID.Value));
            u.Password = Global.ToEncoding(txtPass1.Text.Trim());
            dx.SubmitChanges();
        }

        void bindata()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.UserDBs
                       where k.ID == int.Parse(hdfUserID.Value)
                       select k;
            foreach (UserDB u in list)
            {
                txtAdress.Text = u.address;
                txtCMND.Text = u.cmnd;
                txtChidinh.Text = u.Fullname;
                txtEmai.Text = u.email;
                txtfisnam.Text = u.Lastname;
                txtIntroduct.Text = u.nguoigioithieu;
                txtPhone.Text = u.phone;
                txtTen.Text = u.FirtName;
                txtTennganhang.Text = u.officename;
                txtUser.Text = u.UserName;
                txtUser.Enabled = false;
                txtVisa.Text = u.visa;
                cbCapbac.Items.FindByValue(u.capbac).Selected = true;
                cbGioitinh.Items.FindByValue(u.gioitinh.ToString()).Selected = true;
                cbNgay.Items.FindByValue(u.ngaysinh.ToString()).Selected = true;
                cbNam.Items.FindByValue(u.namsinh.ToString()).Selected = true;
                cbThang.Items.FindByValue(u.thangsinh.ToString()).Selected = true;
                if (u.kichhoat == true)
                    cbCapbac.Enabled = false;
                txtIntroduct.Enabled = false;
                txtChidinh.Enabled = false;
            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {

            DataClasses1DataContext dx = new DataClasses1DataContext();
           
           
            UserDB u = new UserDB();
            u = dx.UserDBs.Single(p => p.ID == int.Parse(hdfUserID.Value));
            u.address = txtAdress.Text;
            //u.bandau = cbCapbac.Text;
            u.capbac = cbCapbac.Text;
            u.cmnd = txtCMND.Text;
            u.email = txtEmai.Text;
            u.FirtName = txtTen.Text;
            u.Lastname = txtfisnam.Text;
            if (cbCapbac.Text == "Cộng Tác Viên")
                u.maCB = "CTV";
            u.namsinh = int.Parse(cbNam.Value.ToString());
            u.ngaysinh = int.Parse(cbNgay.Value.ToString());
            u.thangsinh = int.Parse(cbThang.Value.ToString());
            u.visa = txtVisa.Text;
            u.phone = txtPhone.Text;
            if (FileUpload1.HasFile)
            {
                string file = SavePostedFile(FileUpload1);
                u.photo = file;
            }
            dx.SubmitChanges();
        }


        string SavePostedFile(System.Web.UI.WebControls.FileUpload uploadedFile)
        {
            if (uploadedFile.FileName == "")
                return string.Empty;

            Random r = new Random(9999);
            int n = r.Next(999999);
            string ThumbnailFileName = n + FileUpload1.FileName;
            string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
            using (Image original = Image.FromStream(uploadedFile.FileContent))
            using (Image thumbnail = PhotoUtils.Inscribe(original, 200))
            {
                PhotoUtils.SaveToJpeg(thumbnail, fileName);
            }
            return ThumbnailFileName;
        }
    }
}